#include "ruledialog.h"
#include "ui_ruledialog.h"
#include "RApp.h"
#include "RPrApp.h"
#include "NavApp.h"
#include "Agent.h"
#include "Proposition.h"
#include "AtomicAction.h"
#include "Util.h"
#include "RKernel.h"
#include <QMessageBox>

RuleDialog::RuleDialog(QWidget *parent, Rule *rule) :
    QDialog(parent),
    ui(new Ui::RuleDialog)
{
    ui->setupUi(this);
    mRule = rule;
    if (mRule != NULL) {
        mIsEdit = true;
        switch (mRule->getRuleType()) {
        case PRAPP:
            ui->mLabel1->setText(tr("Agent:"));
            ui->mLabel2->setText(tr("Proposition:"));
            ui->mLabel3->setText(tr("Proposition:"));
            ui->comboBox->setCurrentIndex(0);
            break;
        case APP:
            ui->mLabel1->setText(tr("Agent:"));
            ui->mLabel2->setText(tr("Action:"));
            ui->mLabel3->setText(tr("Action:"));
            ui->comboBox->setCurrentIndex(1);
            break;
        case NAVAPP:
            ui->mLabel1->setText(tr("Proposition:"));
            ui->mLabel2->setText(tr("Action:"));
            ui->mLabel3->setText(tr("Proposition:"));
            ui->comboBox->setCurrentIndex(2);
            break;
        case KERNEL:
            ui->mLabel1->setText(tr("Action:"));
            ui->mLabel2->setText(tr("Proposition:"));
            ui->mLabel3->setVisible(false);
            ui->mLineEdit3->setVisible(false);
            ui->comboBox->setCurrentIndex(3);
            break;
        default:
            ui->mLabel1->setText(tr("Agent:"));
            ui->mLabel2->setText(tr("Proposition:"));
            ui->mLabel3->setText(tr("Proposition:"));
            ui->comboBox->setCurrentIndex(0);
        }
    } else {
        ui->mLabel1->setText(tr("Agent:"));
        ui->mLabel2->setText(tr("Proposition:"));
        ui->mLabel3->setText(tr("Proposition:"));
        ui->comboBox->setCurrentIndex(0);
        mIsEdit = false;
    }
}

RuleDialog::~RuleDialog()
{
    delete ui;
}

void RuleDialog::on_comboBox_currentIndexChanged(int index)
{
    if (index == 0) {
        ui->mLabel1->setText(tr("Agent:"));
        ui->mLabel2->setText(tr("Proposition:"));
        ui->mLabel3->setText(tr("Proposition:"));
        ui->mLabel3->setVisible(true);
        ui->mLineEdit3->setVisible(true);
    } else if (index == 1) {
        ui->mLabel1->setText(tr("Agent:"));
        ui->mLabel2->setText(tr("Action:"));
        ui->mLabel3->setText(tr("Action:"));
        ui->mLabel3->setVisible(true);
        ui->mLineEdit3->setVisible(true);
    } else if (index == 2) {
        ui->mLabel1->setText(tr("Proposition:"));
        ui->mLabel2->setText(tr("Action:"));
        ui->mLabel3->setText(tr("Proposition:"));
        ui->mLabel3->setVisible(true);
        ui->mLineEdit3->setVisible(true);
    } else if (index == 3) {
        ui->mLabel1->setText(tr("Action:"));
        ui->mLabel2->setText(tr("Proposition:"));
        ui->mLabel3->setVisible(false);
        ui->mLineEdit3->setVisible(false);
    }
}



void RuleDialog::on_buttonBox_accepted()
{
    std::cout << "WTF" << std::endl;
    Proposition* prop1 = createProposition(ui->mLineEdit2->text().toStdString());
    Proposition* prop2 = createProposition(ui->mLineEdit3->text().toStdString());
    if (prop1 == NULL || ui->comboBox->currentText().compare("Kernel") == 0? false : (prop2 == NULL)) {
        QMessageBox msgBox;
        msgBox.setText("Illegal expressions!");
        msgBox.setIcon(QMessageBox::Critical);
        msgBox.exec();
        return;
    }
    QString text = ui->comboBox->currentText();
    std::cout << text.toStdString() << std::endl;
    if (text.compare(QString("PrApp")) == 0) {
        std::cout << "WTF" << std::endl;
        if (!prop1->isAction() && !prop2->isAction()) {
            Agent * agent = new Agent(ui->mLineEdit1->text().toStdString());
            mRule = new R_PrApp(agent, prop1, prop2);
            if (mIsEdit) {
                emit rule_edited(mRule);
            } else {
                emit rule_created(mRule);
            }
        }
    } else if (text.compare(QString("App"))== 0) {
        if (prop1->isAction() && prop2->isAction()) {
            Agent * agent = new Agent(ui->mLineEdit1->text().toStdString());
            mRule = new R_App(agent, prop1, prop2);
            if (mIsEdit) {
                emit rule_edited(mRule);
            } else {
                emit rule_created(mRule);
            }
        }
    } else if (text.compare(QString("NavApp"))== 0) {
        Proposition *prop = createProposition(ui->mLineEdit1->text().toStdString());
        if (prop!= NULL && prop->getPropName() == ATOMIC && prop1->isAction() && !prop2->isAction()) {
            mRule = new NavApp((AtomicProposition*)prop, (AtomicAction*)prop1, prop2);
            if (mIsEdit) {
                emit rule_edited(mRule);
            } else {
                emit rule_created(mRule);
            }
        }
    } else if (text.compare(QString("Kernel"))== 0) {
        Proposition *prop = createProposition(ui->mLineEdit1->text().toStdString());
        if (prop!= NULL && prop->getPropName() == ACTION && !prop1->isAction()) {
            mRule = new R_Kernel((AtomicAction*)prop, prop1);
            if (mIsEdit) {
                emit rule_edited(mRule);
            } else {
                emit rule_created(mRule);
            }
        }
    }
    this->accept();
}

void RuleDialog::on_buttonBox_rejected()
{
    this->reject();
}
